import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyCalFrame extends JFrame {
    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JTextField textField1;
    private JTextField textField2;

    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton buttonAdd;
    private JButton buttonSub;
    private JButton buttonMul;
    private JButton buttonDiv;
    private JButton buttonClear;
    private JButton buttonBackSpace;
    private JButton buttonPoint;
    private JButton buttonEqual;
    private Expression expression;

    public MyCalFrame()  {
        this.setLayout(new BorderLayout());
        this.add(panel1, BorderLayout.NORTH);
        panel1.setLayout(new GridLayout(2,2));
        textField1 = new JTextField();
        textField1.setFont(new Font("Serif", Font.PLAIN, 36));
        textField1.setHorizontalAlignment(SwingConstants.RIGHT);
        textField1.setEditable(false);


        textField2 = new JTextField();
        textField2.setFont(new Font("Serif", Font.PLAIN, 36));//设置字体，加粗，字号
        textField2.setEditable(false);//设置编辑
        textField2.setHorizontalAlignment(SwingConstants.RIGHT);//设置对齐方式
        panel1.add(textField1);
        panel1.add(textField2);

        this.add(panel2, BorderLayout.CENTER);
        panel2.setLayout(new GridLayout(3,4));

        button1 = new JButton("1");
        button1.setFont(new Font("Serif", Font.PLAIN, 36));
        button2 = new JButton("2");
        button2.setFont(new Font("Serif", Font.PLAIN, 36));

        button3 = new JButton("3");
        button3.setFont(new Font("Serif", Font.PLAIN, 36));

        button4 = new JButton("4");
        button4.setFont(new Font("Serif", Font.PLAIN, 36));
        buttonAdd = new JButton("+");
        buttonAdd.setFont(new Font("Serif", Font.PLAIN, 36));
        buttonSub = new JButton("-");
        buttonSub.setFont(new Font("Serif", Font.PLAIN, 36));
        buttonMul = new JButton("*");
        buttonMul.setFont(new Font("Serif", Font.PLAIN, 36));
        buttonDiv = new JButton("/");
        buttonDiv.setFont(new Font("Serif", Font.PLAIN, 36));
        buttonClear = new JButton("C");
        buttonClear.setFont(new Font("Serif", Font.PLAIN, 36));
        buttonBackSpace = new JButton("B");
        buttonBackSpace.setFont(new Font("Serif", Font.PLAIN, 36));
        buttonPoint = new JButton(".");
        buttonPoint.setFont(new Font("Serif", Font.PLAIN, 36));
        buttonEqual = new JButton("=");
        buttonEqual.setFont(new Font("Serif", Font.PLAIN, 36));

        panel2.add(button1);
        panel2.add(button2);
        panel2.add(button3);
        panel2.add(button4);
        panel2.add(buttonAdd);
        panel2.add(buttonSub);
        panel2.add(buttonMul);
        panel2.add(buttonDiv);
        panel2.add(buttonClear);
        panel2.add(buttonBackSpace);
        panel2.add(buttonPoint);
        panel2.add(buttonEqual);



        this.setSize(800, 600);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            textField2.setText(textField2.getText() +"1");

        }
    });
    button2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            textField2.setText(textField2.getText() +"2");
        }
    });
    button3.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            textField2.setText(textField2.getText() +"3");
        }
    });
    button4.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            textField2.setText(textField2.getText() +"4");
        }
    });

    //初始化表达式
        expression = new Expression();
        buttonAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                NumSign numSign = new NumSign(textField2.getText(),'+');
                textField1.setText(textField1.getText() + textField2.getText()+ '+');
                textField2.setText("");
                expression.add(numSign);
            }
        });
        buttonSub.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                NumSign numSign = new NumSign(textField2.getText(),'-');
                textField1.setText(textField1.getText() + textField2.getText() + '-');
                textField2.setText("");
                expression.add(numSign);
            }
        });
        buttonMul.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                NumSign numSign = new NumSign(textField2.getText(),'*');
                textField1.setText(textField1.getText() + textField2.getText()+'*');
                textField2.setText("");
                expression.add(numSign);
            }
        });
        buttonDiv.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                NumSign numSign = new NumSign(textField2.getText(),'/');
                textField1.setText(textField1.getText() + textField2.getText() + '/');
                textField2.setText("");
                expression.add(numSign);
            }
        });

        buttonEqual.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                NumSign numSign = new NumSign(textField2.getText(),'=');
                textField1.setText(textField1.getText() + textField2.getText() + '=');
                textField2.setText("");
                expression.add(numSign);
                textField2.setText(expression.getResult());
                expression.clear();
            }
        });
buttonClear.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        expression.clear();
        textField2.setText("");
        textField1.setText("");
    }
});
buttonBackSpace.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent actionEvent) {

    }
});

    }

}
